package com.itheima.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.User;
import com.itheima.domain.UserList;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/tt")
    public String test01(){
        return "aa";
    }
    //测试modelAndView
    @RequestMapping("/quick2")
    public ModelAndView test03(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("username","yyy");
        modelAndView.setViewName("aa");
        return modelAndView;
    }
    @RequestMapping(value = "/quick3")
    public ModelAndView test03(ModelAndView modelAndView){
        modelAndView.addObject("username","itheima");
        modelAndView.setViewName("success");
        return modelAndView;
    }
    @RequestMapping(value = "/quick4")
    public String test04(Model model){
        model.addAttribute("username","博学谷");
        return "success";
    }
    @RequestMapping(value = "/quick5")
    public String test05(HttpServletRequest request){
        request.setAttribute("username","酷丁鱼");
        return "success";
    }
    @RequestMapping(value = "/quick6")
    public void test06(HttpServletResponse response) throws IOException {
        response.getWriter().write("hello itcast");
    }
    @RequestMapping(value = "/quick7")
    @ResponseBody//告知SpringMVC框架不进行视图跳转,直接响应数据
    public String test07() throws IOException {
        return "hello itheima";
    }
    @RequestMapping(value = "/quick8")
    @ResponseBody//告知SpringMVC框架不进行视图跳转,直接响应数据
    public String test08() throws IOException {
        return "{\"username\":\"zhansan\"},\"age\":18";
    }
    @RequestMapping(value = "/quick9")
    @ResponseBody//告知SpringMVC框架不进行视图跳转,直接响应数据
    public String test09() throws IOException {
        User user = new User();
        user.setUsername("lisi");
        user.setAge(30);
        //使用json转换工具
        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(user);
        return json;
    }
    @RequestMapping(value = "/quick10")
    @ResponseBody//告知SpringMVC框架不进行视图跳转,直接响应数据,配置注解驱动后能直接将对象转换成json
    //期望SpringMVC自动将user转换成json格式的字符串
    public User test10() throws IOException {
        User user = new User();
        user.setUsername("lisi");
        user.setAge(30);
        //使用json转换工具
        return user;
    }
    @RequestMapping(value = "/quick11")
    @ResponseBody//告知SpringMVC框架不进行视图跳转,直接响应数据,配置注解驱动后能直接将对象转换成json
    //期望SpringMVC自动将user转换成json格式的字符串
    public void test11(String username,int age)  {
        System.out.println(username);
        System.out.println(age);
    }
    @RequestMapping(value = "/quick12")
    @ResponseBody//告知SpringMVC框架不进行视图跳转,直接响应数据,配置注解驱动后能直接将对象转换成json
    //期望SpringMVC自动将user转换成json格式的字符串
    public void test12(User user)  {
        System.out.println(user);
    }
    @RequestMapping(value = "/quick13")
    @ResponseBody//告知SpringMVC框架不进行视图跳转,直接响应数据,配置注解驱动后能直接将对象转换成json
    //期望SpringMVC自动将user转换成json格式的字符串
    public void test13(String[] strs)  {
        System.out.println(Arrays.asList(strs));
    }
    @RequestMapping(value = "/quick14")
    @ResponseBody//告知SpringMVC框架不进行视图跳转,直接响应数据,配置注解驱动后能直接将对象转换成json
    //期望SpringMVC自动将user转换成json格式的字符串
    public void test14(UserList userList)  {
        System.out.println(userList);
    }
    @RequestMapping(value = "/quick15")
    @ResponseBody//告知SpringMVC框架不进行视图跳转,直接响应数据,配置注解驱动后能直接将对象转换成json
    //期望SpringMVC自动将user转换成json格式的字符串
    public void test15(@RequestBody List<User> userList)  {
        System.out.println(userList);
    }
    @RequestMapping(value = "/quick16")
    @ResponseBody//告知SpringMVC框架不进行视图跳转,直接响应数据,配置注解驱动后能直接将对象转换成json
    //期望SpringMVC自动将user转换成json格式的字符串
    public void test16(@RequestParam(value = "name",required = false,defaultValue = "itcast") String username)  {
        System.out.println(username);
    }
    @RequestMapping(value = "/quick17/{username}")
    @ResponseBody//告知SpringMVC框架不进行视图跳转,直接响应数据,配置注解驱动后能直接将对象转换成json
    //期望SpringMVC自动将user转换成json格式的字符串
    public void test17(@PathVariable(value = "username") String username)  {
        System.out.println(username);
    }
    @RequestMapping(value = "/quick18")
    @ResponseBody//告知SpringMVC框架不进行视图跳转,直接响应数据,配置注解驱动后能直接将对象转换成json
    //期望SpringMVC自动将user转换成json格式的字符串
    public void test18(Date date)  {
        System.out.println(date);
    }
    @RequestMapping(value = "/quick19")
    @ResponseBody//告知SpringMVC框架不进行视图跳转,直接响应数据,配置注解驱动后能直接将对象转换成json
    //期望SpringMVC自动将user转换成json格式的字符串
    public void test19(HttpServletResponse response, HttpServletRequest request, HttpSession session)  {
        System.out.println(request);
        System.out.println(response);
        System.out.println(session);
    }
    @RequestMapping(value = "/quick21")
    @ResponseBody//告知SpringMVC框架不进行视图跳转,直接响应数据,配置注解驱动后能直接将对象转换成json
    //期望SpringMVC自动将user转换成json格式的字符串
    public void test21(@CookieValue(value = "JSESSIONID") String jsessionId)  {
        System.out.println(jsessionId);
    }

    @RequestMapping(value = "/quick22")
    @ResponseBody//告知SpringMVC框架不进行视图跳转,直接响应数据,配置注解驱动后能直接将对象转换成json
    //期望SpringMVC自动将user转换成json格式的字符串
    public void test22(String filename,MultipartFile multipartFile) throws IOException {
        System.out.println(filename);
        //获取文件上传名称
        String originalFilename = multipartFile.getOriginalFilename();
        multipartFile.transferTo(new File("E:\\fileUpload\\"+originalFilename));
    }
    @RequestMapping(value = "/quick23")
    @ResponseBody//告知SpringMVC框架不进行视图跳转,直接响应数据,配置注解驱动后能直接将对象转换成json
    //期望SpringMVC自动将user转换成json格式的字符串
    public void test23(String filename,MultipartFile[] multipartFile) throws IOException {
        System.out.println(filename);
        //获取文件上传名称
        for (MultipartFile file : multipartFile) {
            String originalFilename = file.getOriginalFilename();
            file.transferTo(new File("E:\\fileUpload\\"+originalFilename));
        }
    }


}
